import 'dart:ui';

import 'package:flutter/material.dart';

class CanvasCenterUtils {
  static drawGrid(Canvas canvas, Paint paint, Size size) {
    // 绘制四分之一的网格 ,要画全屏的画需要用镜像的方法
    //如果是相同或者对称的对象，可以通过缩放进行对称变化。
    // 沿x轴镜像，就相当于canvas.scale(1, -1)；
    // 沿y轴镜像，就相当于canvas.scale(-1, 1)；
    // 沿原点镜像，就相当于canvas.scale(-1, -1)；
    drawHalfGrid(canvas, paint, size);
    // x轴镜像
    canvas.save();
    canvas.scale(1, -1);
    drawHalfGrid(canvas, paint, size);
    canvas.restore();
    // y轴镜像
    canvas.save();
    canvas.scale(-1, 1);
    drawHalfGrid(canvas, paint, size);
    canvas.restore();
    // 原点镜像
    canvas.save();
    canvas.scale(-1, -1);
    drawHalfGrid(canvas, paint, size);
    // canvas.drawLine(Offset(0, 0), Offset(100, 100), paint);
    canvas.restore();
  }

  static drawHalfGrid(Canvas canvas, Paint paint, Size size) {
    // 画网格
    double step = 20;
    paint
      ..color = Colors.grey
      ..strokeWidth = 1
      ..style = PaintingStyle.stroke;

    // 绘制横线
    canvas.save();
    // 计算绘制的 1/4 屏幕能画多少条线，然后循环就可以了
    for (int i = 0; i < size.height / 2 / step; i++) {
      canvas.drawLine(Offset(0, 0), Offset(size.width / 2, 0), paint);
      canvas.translate(0, step);
    }
    canvas.restore();
    // 绘制竖线
    canvas.save();
    // 计算绘制的 1/4 屏幕能画多少条线，然后循环就可以了
    for (int i = 0; i < size.width / 2 / step; i++) {
      canvas.drawLine(Offset(0, 0), Offset(0, size.height / 2), paint);
      canvas.translate(step, 0);
    }
    canvas.restore();
  }

  static drawCoor(Canvas canvas, Paint _paint, Size size) {
    _paint
      ..color = Colors.blue
      ..strokeWidth = 2;
    Offset yEndPoint = Offset(0, size.height / 2);
    canvas.drawLine(Offset(0, -size.height / 2), yEndPoint, _paint);

    canvas.drawLine(yEndPoint, Offset(-10, size.height / 2 - 10), _paint);
    canvas.drawLine(yEndPoint, Offset(10, size.height / 2 - 10), _paint);

    Offset xEndPoint = Offset(size.width / 2, 0);
    canvas.drawLine(Offset(-size.width / 2, 0), xEndPoint, _paint);
    canvas.drawLine(xEndPoint, Offset(size.width / 2 - 10, -10), _paint);
    canvas.drawLine(xEndPoint, Offset(size.width / 2 - 10, 10), _paint);
  }
}
